<?php

/**
 * Show vote.
 */
function vote_show($node, $cid = NULL) {
  //drupal_set_title(check_plain($node->title));
  if ($node->allowvotes) {
    $show = drupal_get_form ('poll_view_voting', $node, '');
  } else {
    $show = poll_view_results($node, '', '', 'block');
  }
  /*
  require_once(drupal_get_path('module', 'poll') . '/poll.pages.inc');
  $show = poll_results($node);
  return node_show($node, $cid);
  */
  return $show;
}

function vote_list_recent() {
  global $user;
  $sql = db_rewrite_sql("SELECT n.nid, n.title, p.active, n.created, SUM(c.chvotes) AS votes FROM {node} n INNER JOIN {poll} p ON n.nid = p.nid INNER JOIN {poll_choices} c ON n.nid = c.nid WHERE n.status = 1 GROUP BY n.nid, n.title, p.active, n.created ORDER BY n.created DESC");
  // Count all polls for the pager.
  $count_sql = db_rewrite_sql("SELECT COUNT(*) FROM {node} n INNER JOIN {poll} p ON n.nid = p.nid WHERE n.status = 1");
  $result = pager_query($sql, 10, 0, $count_sql);
  $output = '<ul class="vote-ul">';
  while ($node = db_fetch_object($result)) {
    $output .= '<li><div class="vote-item">'
        . '<div class="vote-item-title"><h5>'. l($node->title, "vote/show/$node->nid") .'</h5></div>'
        . '<div class="vote-item-footer">'
          . '<span class="vote-count">' . format_plural($node->votes, '1 vote', '@count votes') .'</span>'
          . ($node->active ? ('<span class="vote-status-open">' . t('open') . '</span>') : ('<span class="vote-status-closed">' . t('closed') . '</span>'))
        .'</div>'
    .'</div></li>';
  }
  $output .= '</ul>';
  $output .= theme("pager", NULL, 15);

  return $output;
}

function vote_list_hot() {
  $sql = db_rewrite_sql("SELECT n.nid, n.title, p.active, n.created, SUM(c.chvotes) AS votes FROM {node} n INNER JOIN {poll} p ON n.nid = p.nid INNER JOIN {poll_choices} c ON n.nid = c.nid WHERE n.status = 1 GROUP BY n.nid, n.title, p.active, n.created ORDER BY votes DESC");
  // Count all polls for the pager.
  $count_sql = db_rewrite_sql("SELECT COUNT(*) FROM {node} n INNER JOIN {poll} p ON n.nid = p.nid WHERE n.status = 1");
  $result = pager_query($sql, 10, 0, $count_sql);
  $output = '<ul class="vote-ul">';
  while ($node = db_fetch_object($result)) {
    $output .= '<li><div class="vote-item">'
        . '<div class="vote-item-title"><h5>'. l($node->title, "vote/show/$node->nid") .'</h5></div>'
        . '<div class="vote-item-footer">'
          . '<span class="vote-count">' . format_plural($node->votes, '1 vote', '@count votes') .'</span>'
          . ($node->active ? ('<span class="vote-status-open">' . t('open') . '</span>') : ('<span class="vote-status-closed">' . t('closed') . '</span>'))
        .'</div>'
    .'</div></li>';
  }
  $output .= '</ul>';
  $output .= theme("pager", NULL, 15);

  return $output;
}

function vote_list_friend() {
  require_once(drupal_get_path('module', 'renren') . '/renren.model.inc');
  $friends = renren_get_friends();
  print_pre($friends);
  return "TODO";
}

function vote_list_self() {
  global $user;
  $sql = db_rewrite_sql("SELECT n.nid, n.title, p.active, n.created, SUM(c.chvotes) AS votes FROM {node} n INNER JOIN {poll} p ON n.nid = p.nid INNER JOIN {poll_choices} c ON n.nid = c.nid WHERE n.status = 1 AND n.uid = {$user->uid} GROUP BY n.nid, n.title, p.active, n.created ORDER BY n.created DESC");
  // Count all polls for the pager.
  $count_sql = db_rewrite_sql("SELECT COUNT(*) FROM {node} n INNER JOIN {poll} p ON n.nid = p.nid WHERE n.status = 1 AND n.uid={$user->uid}");
  $result = pager_query($sql, 10, 0, $count_sql);
  $output = '<ul class="vote-ul">';
  while ($node = db_fetch_object($result)) {
    $output .= '<li><div class="vote-item">'
        . '<div class="vote-item-title"><h5>'. l($node->title, "vote/show/$node->nid") .'</h5></div>'
        . '<div class="vote-item-footer">'
          . '<span class="vote-count">' . format_plural($node->votes, '1 vote', '@count votes') .'</span>'
          . ($node->active ? ('<span class="vote-status-open">' . t('open') . '</span>') : ('<span class="vote-status-closed">' . t('closed') . '</span>'))
        .'</div>'
    .'</div></li>';
  }
  $output .= '</ul>';
  $output .= theme("pager", NULL, 15);

  return $output;
}


function vote_create() {
//print_pre(node_get_types());
  require_once(drupal_get_path('module', 'node') . '/node.pages.inc');
  return node_add('poll');
}